package net.qhzw.vip.qiyu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.entity.GuaranteeSite;
import net.qhzw.vip.qiyu.entity.GuaranteeUser;
import net.qhzw.vip.qiyu.entity.SysContract;
import net.qhzw.vip.qiyu.entity.SysUser;
import net.qhzw.vip.qiyu.mapper.GuaranteeUserMapper;
import net.qhzw.vip.qiyu.model.GuaranteeSiteUser;
import net.qhzw.vip.qiyu.model.GuaranteeUserQuery;
import net.qhzw.vip.qiyu.model.SendSmsParam;
import net.qhzw.vip.qiyu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import net.qhzw.vip.qiyu.util.SecurityUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GuaranteeUserServiceImpl extends ServiceImpl<GuaranteeUserMapper, GuaranteeUser> implements GuaranteeUserService {

    @Autowired
    private GuaranteeUserMapper guaranteeUserMapper;

    @Autowired
    private GuaranteeSiteService guaranteeSiteService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysContractService sysContractService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private DtsUserService dtsUserService;

    /***
     * 分页查询站点用户
     * @param page
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Page<GuaranteeUser> page, Long shopId, GuaranteeUserQuery query) {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.getById(userId);
        List<Long> siteIds = null;
        if (sysUser.getCompanyId().longValue() == 1) {
            List<GuaranteeSite> list = guaranteeSiteService.list();
            siteIds = list.stream().map(GuaranteeSite::getId).collect(Collectors.toList());
        } else {
            siteIds = guaranteeSiteService.getSiteIdByUserId(userId);
        }
        query.setSiteIds(siteIds);
        Map<String, Object> result = new HashMap<>();
        int total = 0;
        List<GuaranteeUser> records = new ArrayList<>();
        Long stieId = sysUser.getStieId();
        if (stieId == 1){
            stieId = null;
        }
        if (!CollectionUtils.isEmpty(siteIds)) {
            PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
            List<GuaranteeUser> guaranteeUsers = guaranteeUserMapper.findByPage(shopId, query, stieId);
            if (!CollectionUtils.isEmpty(guaranteeUsers)) {
                for (GuaranteeUser record : guaranteeUsers) {
                    if (record.getSiteId() != null) {
                        String siteName = guaranteeSiteService.findNameById(record.getSiteId());
                        record.setSiteName(siteName);
                    }
                }
            }
            PageInfo<GuaranteeUser> pageInfo = new PageInfo<>(guaranteeUsers);
            total = (int) pageInfo.getTotal();
            records = pageInfo.getList();

        }
        result.put("total", total);
        result.put("records", records);
        return result;
    }

    /***
     * 发送短信通知
     * @param guaranteeUser
     */
    @Override
    public void sendMsg(GuaranteeUser guaranteeUser) {
        if (guaranteeUser.getStatus().intValue() == 1) {
            return;
        }
        Long communityId = guaranteeSiteService.findSiteMobile(guaranteeUser.getId());
        List<String> siteMobile = null;
        if (communityId != null) {
            siteMobile = dtsUserService.getMobileByCommunityId(communityId);
        }
        GuaranteeSiteUser guaranteeSiteUser = guaranteeSiteService.getName(guaranteeUser.getId());
        if (guaranteeSiteUser != null) {
            //发送短信 SysContract::getType, 1
            SysContract contract = sysContractService.getOne(new LambdaQueryWrapper<SysContract>().eq(SysContract::getType, 1));
            if (!StringUtils.isEmpty(contract.getRemindMobile())) {
                String[] split = contract.getRemindMobile().split("/");
                List<String> strings = Arrays.asList(split);
                List<String> all = new ArrayList<>();
                if (!CollectionUtils.isEmpty(siteMobile)) {
                    all.addAll(siteMobile);
                    all.addAll(strings);
                    all.stream().distinct().collect(Collectors.toList());
                }
                log.info("【发送短信通知 mobile】： {}", JSON.toJSONString(all, true));
                for (String mobile : all) {
                    SendSmsParam sendSmsParam = new SendSmsParam(mobile, "SMS_235819772", "");
                    smsService.sendRemind(sendSmsParam, guaranteeSiteUser);
                }
            }
        }
    }


    /**
     * 禁用担保用户
     *
     * @param idCard
     */
    @Override
    public void disableGuarantee(String idCard) {
        guaranteeUserMapper.disableGuarantee(idCard);
    }
}
